//获取系统时间
var newDate = '';
getLangDate();

//值小于10时，在前面补0
function dateFilter(date) {
    if (date < 10) {
        return "0" + date;
    }
    return date;
}

function getLangDate() {
    var obj = new Date(); //表示当前系统时间的Date对象
    var year = obj.getFullYear(); //当前系统时间的完整年份值
    var month = obj.getMonth() + 1; //当前系统时间的月份值
    var date = obj.getDate(); //当前系统时间的月份中的日
    var day = obj.getDay(); //当前系统时间中的星期值
    var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    var week = weeks[day]; //根据星期值，从数组中获取对应的星期字符串
    var hour = obj.getHours(); //当前系统时间的小时值
    var minute = obj.getMinutes(); //当前系统时间的分钟值
    var second = obj.getSeconds(); //当前系统时间的秒钟值
    var timeValue = "" + ((hour >= 12) ? (hour >= 18) ? "晚上" : "下午" : "上午"); //当前时间属于上午、晚上还是下午
    newDate = dateFilter(year) + "年" + dateFilter(month) + "月" + dateFilter(date) + "日 " + " " + dateFilter(hour) + ":" + dateFilter(minute) + ":" + dateFilter(second);
    document.getElementById("sysTime").innerHTML = timeValue + "好！欢迎使用报修平台。当前时间为：" + newDate + "　" + week;
    setTimeout("getLangDate()", 1000);
}

layui.use(['table', 'jquery'], function () {
        var table = layui.table,
            $ = layui.jquery;


        //我的指派任务
        var myAssign = table.render({
            elem: "#myAssign",
            url: '/reListController/myAssign',
            cellMinWidth: 95,
            limit: 10,
            limits: [5, 10, 15, 20, 25],
            page: true,
            loading: true,
            size: 'lg',
            response: {
                statusName: 'code'
                , statusCode: 200
                , msgName: 'msg'
                , countName: 'count'
                , dataName: 'data'
            },
            id: 'myAssign',
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'id', title: 'ID', hide: true},
                {field: 'userId', title: '报修人ID', align: 'center', width: 100},
                {field: 'userName', title: '姓名', align: 'center', width: 100},
                {field: 'jobId', title: '工号', align: 'center', width: 100},
                {field: 'userTel', title: '电话', align: 'center', width: 150},
                {field: 'location', title: '报修位置', align: 'center'},
                {
                    field: 'emergency', title: '紧急程度', align: 'center', width: 100, templet: function (d) {
                        if (d.emergency === 1) {
                            return '<span style="color: red;font-weight: 600">顶级</span>'
                        } else if (d.emergency === 2) {
                            return '<span style="color: brown;font-weight: 600">高级</span>'
                        } else if (d.emergency === 3) {
                            return '<span style="color: orange;font-weight: 600">中级</span>'
                        } else {
                            return '<span style="color: dodgerblue;font-weight: 600">低级</span>'
                        }
                    }
                },
                {field: 'detailInfo', title: '描述信息', align: 'center'},
                {field: 'startTime', title: '报修时间', align: 'center'},
                {field: 'assignTime', title: '指派时间', align: 'center'},
                {
                    field: 'state', title: '是否解决', align: 'center', templet: function (d) {
                        if (d.state === 0) {
                            return '<span style="color: red">待处理</span>'
                        } else if (d.state === 1) {
                            return '<span style="color: yellow">处理中</span>'
                        } else {
                            return '<span style="color: green">已处理</span>'
                        }
                    }
                },
                {field: 'endTime', title: '解决时间', align: 'center'},
                {
                    field: 'photo', title: '图片描述', align: 'center', width: 150, templet: function (d) {
                        return '<a href="' + d.photo + '" target="_blank"><img src="' + d.photo + '" style="width: 60px;height: 60px"/></a>'
                    }
                },
                {title: '操作', width: 300, templet: '#myAssignBar', fixed: "right", align: "center"}
            ]]
        });


        table.on('tool(myAssign)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            console.log(data);
            if (layEvent === 'accept') {//监听受理
                if (data.state === 2) {
                    layer.msg("该报修已修复!")
                } else {
                    accept(data);
                }
            } else if (layEvent === 'seeComment') {
                seeComment(data.id);
            } else {
                seeReply(data.id);
            }
        });

    /**
     * 维修人员查看回复
     */
    function seeReply(reId) {

        $.ajax({
            url:'/replyController/getReplyByReListId',
            type:'GET',
            data: {reId:reId},
            success:function (res) {
                if (res.code === 200){
                    var ele ="";
                    ele += res.data.replyContent;
                    $("#seeReply").append(ele);
                }
            }
        })

        var index = layer.open({
            title: "解决回复",
            type: 1,
            area: ['30%', '35%'],
            content: $('#seeReply'),
            cancel: function (index, layero) {
                layer.close(index)
                $('#seeReply').remove();
                window.location.reload();
                return false;
            }
        });
    }


        /**
         * 受理
         */
        function accept(obj) {
            /*var index = layer.open({
                title: "解决回复",
                type: 2,
                area: ['30%', '35%'],
                content: './addReply.html',
                success: function (layero, index) {
                    var body = layui.layer.getChildFrame('body', index);
                    if (obj) {
                        body.find('.reId').val(obj.id);
                        form.render();
                    }
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500)

                }
            })*/
            layer.confirm('确定已经修复好了吗？', {icon: 3, title: '提示信息'}, function (index) {
                layer.open({
                    title: "解决回复",
                    type: 2,
                    area: ['30%', '35%'],
                    content: './addReply.html',
                    success: function (layero, index) {
                        var body = layui.layer.getChildFrame('body', index);
                        if (obj) {
                            body.find('.reId').val(obj.id);
                            form.render();
                        }
                        setTimeout(function () {
                            layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        }, 500)

                    }
                });
                /*layer.confirm('确定已经修复好了吗？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: '/reListController/update',
                        type: 'POST',
                        data: JSON.stringify(obj),
                        dataType: 'json',
                        contentType: 'application/json',
                        success: function (res) {
                            if (res.data != null || res.data > 0) {
                                myAssign.reload();
                                layer.close(index);
                                layer.msg("受理成功!")
                            } else {
                                layer.msg("受理失败!")
                            }
                        }
                    });
                });*/
            })
        }

        /**
         * 查看评论
         */
        function seeComment(reId) {
            console.log("评论详情");
            console.log(reId);

            $.ajax({
                url: '/commentController/getCommentByReListId',
                type: 'GET',
                data: {reId: reId},
                success: function (res) {
                    if (res.code === 200) {
                        var ele = "";
                        $.each(res.data, function (index, item) {
                            ele += " <div class=\"layui-row layui-col-space10\">\n" +
                                "        <span class=\"layui-col-xs10 layui-col-xs-offset1\">" + item.comments + "</span>\n" +
                                "    </div>";
                        });
                        $("#seeComments").append(ele);
                    }
                }
            })

            var index = layer.open({
                title: "评论列表",
                type: 1,
                area: ['30%', '35%'],
                content: $('#seeComments'),
                cancel: function (index, layero) {
                    layer.close(index)
                    $('#seeComments').remove();
                    window.location.reload();
                    return false;
                }
            });
        }


    }
);